<template>
  <div class='container'>
    <el-card>
      <el-row  type="flex" justify="space-between" align="center">
        <!-- 左侧 -->
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="目录名称">
            <el-input v-model.trim="formInline.directoryName"></el-input>
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="formInline.state" placeholder="请选择">
              <el-option label="启用" :value='1'></el-option>
              <el-option label="禁用" :value='0'></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button size="small" @click="clearFn">清除</el-button>
            <el-button type="primary" size="small" @click="searchFn">搜索</el-button>
            <!-- <el-button type="primary" size="small" @click="ccc">重置</el-button> -->
          </el-form-item>
        </el-form>
        <div class="right">
          <!-- 右侧 -->
          <el-button @click="goBackFn" v-if="show">⬅️ 返回学科</el-button>
          <el-button @click="showDialog = true" type="success" icon="el-icon-edit" size="small">新增目录</el-button>
        </div>
      </el-row>
      <el-alert type="info" show-icon :closable="false">数据一共{{total}}条</el-alert>
      <template>
        <el-table
          :data="tableData"
          style="width: 100%;margin-top: 15px">
          <el-table-column
            type="index"
            label="序号"
            align="center"
            :index="indexFn"
            width="60">
          </el-table-column>
          <el-table-column
            prop="subjectName"
            label="所属学科"
            align="center"
            width="130">
          </el-table-column>
          <el-table-column
            prop="directoryName"
            label="目录名称"
            align="center"
            width="150">
          </el-table-column>
          <el-table-column
            prop="username"
            label="创建者"
            align="center"
            width="150">
          </el-table-column>
          <el-table-column
          v-slot='{ row }'
            label="创建日期"
            align="center"
            width="200">
            <template>
              {{ row.addDate | parseTimeByString()}}
            </template>
          </el-table-column>
          <el-table-column
            prop="totals"
            label="面试题数量"
            align="center"
            width="160">
          </el-table-column>
          <el-table-column
            v-slot="{ row }"
            label="状态"
            align="center"
            width="120">
            {{ row.state === 1 ? '已启用' : '已禁用'}}
          </el-table-column>
          <el-table-column
            align="center"
            v-slot="{ row }"
            label="操作">
            <template>
              <el-button type="text" size="small" @click="changeFn(row)" >{{ row.state === 1 ? '禁用' : '启用' }}</el-button>
              <el-button type="text" size="small" @click="showUpdate(row)" :disabled=" row.state === 0 ? false : true">修改</el-button>
              <el-button type="text" size="small" @click="delFn(row)" :disabled=" row.state === 0 ? false : true">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
       <!-- 分页 -->
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="pagesize"
          layout="prev, pager, next, sizes, jumper"
          :total="total">
        </el-pagination>
      </div>
      <DirectorysAdd :id="id" @getList="getListFn" ref="children" :showDialog.sync="showDialog" ></DirectorysAdd>
    </el-card>
  </div>
</template>

<script>
import { list, changeState, remove } from '@/api/hmmm/directorys'
import DirectorysAdd from '../components/directorys-add.vue'
export default {
  components: { DirectorysAdd },
  data () {
    return {
      formInline: {
        directoryName: '',
        state: null
      },
      tableData: [],
      // ---分页相关---
      // 当前页
      currentPage: 1,
      // 总条数
      total: 0,
      // 当前页面显示个数
      pagesize: 5,
      // 是否显示新增窗口
      showDialog: false,
      show: false,
      id: null
    }
  },
  created () {
    this.getList()
    this.showButton()
  },
  methods: {
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
      this.pagesize = val
      this.getList()
    },
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      this.currentPage = val
      this.getList()
    },
    // 序号
    indexFn (index) {
      return index + 1 + this.pagesize * (this.currentPage - 1)
    },
    // 获取目录列表
    async getList () {
      // console.log(this.id)
      const data = await list({
        // 当前页
        page: this.currentPage,
        // 页面显示个数
        pagesize: this.pagesize,
        // 学科ID
        subjectID: this.id,
        // 目录名称
        directoryName: this.formInline.directoryName,
        // 状态ID
        state: this.formInline.state
      })
      this.tableData = data.data.items
      this.total = data.data.counts
    },
    // 清空输入框
    clearFn () {
      this.formInline.directoryName = null
      this.formInline.state = null
      // this.getList()
    },
    // 搜索对应数据
    async searchFn () {
      this.currentPage = 1
      // const data = await list({
      //   // 当前页
      //   page: this.currentPage,
      //   // 页面显示个数
      //   pagesize: this.pagesize,
      //   // 目录名称
      //   directoryName: this.formInline.directoryName,
      //   // 状态ID
      //   state: this.formInline.state
      // })
      const data = this.getList()
      this.tableData = data.data.items
      this.total = data.data.counts
    },
    // 状态变化
    async changeFn (data) {
      await changeState({
        id: data.id,
        state: data.state === 1 ? 0 : 1
      })
      this.getList()
      this.$message.success('操作成功！')
    },
    // 删除目录
    async delFn (data) {
      // console.log('1')
      try {
        await this.$confirm('确认删除？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
      } catch {
        return
      }
      await remove(data)
      this.getList()
      this.$message.success('操作成功！')
    },
    // 回显数据
    async showUpdate (row) {
      this.showDialog = true
      this.$refs.children.form = { ...row }
    },
    getListFn () {
      this.getList()
    },
    showButton () {
      this.id = this.$route.query.id
      this.getList()
      // console.log(this.id)
      if (this.id) {
        this.show = true
      }
      this.getList()
    },
    goBackFn () {
      this.$router.push('list')
    }
  }
}
</script>

<style scoped lang='less'>
.container {
  margin: 5px;
  label {
    margin-left: 30px;
  }
}
.block {
  margin-top: 10px;
  float: right;
  margin-right: 35px;
}
</style>
